package models

import (
	"shuqi-novel/novelrpc/novelrpc"

	"gorm.io/gorm"
)

type User struct {
	gorm.Model
	Account   string `gorm:"type:char(11);not null;"`
	Password  string `gorm:"type:char(32);not null;"`
	HeadImage string `gorm:"type:varchar(500);not null;"`
	NickName  string `gorm:"type:varchar(30);not null;"`
}

type Book struct {
	gorm.Model
	Title  string `gorm:"type:varchar(30);not null;"`
	Image  string `gorm:"type:varchar(500);not null;"`
	Desc   string `gorm:"type:varchar(300);not null;"`
	UserId int64  `gorm:"type:int(11);not null;"`
}

func (b *Book) BookList(db *gorm.DB, books []*novelrpc.BookList) ([]*novelrpc.BookList, error) {
	err2 := db.Debug().Model(&Book{}).
		Select("books.* books.id AS book_id,books.title,books.image,books.DESC,users.id AS user_id,users.head_image AS user_image,users.nick_name AS user_name").
		Joins("LEFT JOIN users ON users.id = books.user_id").
		Find(&books).Error

	return books, err2
}
